﻿<%@ WebHandler Language="C#" Class="upload" %>

using System;
using System.Web;

public class upload : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        int i = 1;
        context.Response.ContentType = "text/plain";
        HttpPostedFile file = context.Request.Files["FileData"];
        string uploadpath = HttpContext.Current.Server.MapPath(context.Request["folder"]) + "\\";
        string now = DateTime.Now.ToString("yyyy/MM/dd/");
        uploadpath += now;
        string suff = System.IO.Path.GetExtension(file.FileName);
        string name = System.IO.Path.GetFileNameWithoutExtension(file.FileName);
        string filename = name;
        while (System.IO.File.Exists(uploadpath + filename + suff))
        {
            filename = name + "(" + i.ToString() + ")";
            i++;    
        }
        if (file != null)
        {
            Cstudy.Tools.DirectoryHelp.Create(uploadpath);
            file.SaveAs(uploadpath + filename + suff);
            Cstudy.MODEL.attachment att = new Cstudy.MODEL.attachment();
            att.ResID = int.Parse(context.Request.Params["resid"]);
            att.Size = file.ContentLength / 1024;
            att.Name = filename;
            att.Url = uploadpath + filename + suff;
            if (new Cstudy.BLL.attachment().AddAttachment(att))
            {
                context.Response.Write(now + filename + suff);
            }
        }
        else
        {
            context.Response.Write("0");
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}